Repository navigation
fix(deparser): preserve multi-line string literals in pretty mode and parenthesize indirection targets - #378
Conversation
… parenthesize indirection targets
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Review complete. No blocking issues — 1 nitpick below. 🧹 Nitpicks (1) — 🟢 1 low
This PR extends the deparser with two new fixture suites — multiline string literals and parenthesized indirection (subscripting/field selection) — and adjusts output formatting accordingly. The main code changes replace the previous indent logic in the SQL formatter with a token-aware
One low-severity finding remains: an unreachable Reviewed commit: 2b646ac |
There was a problem hiding this comment.
Devin Review found 2 findings. Devin automatically fixed all 2 findings.
Fixed by Devin (2)
- Broken multiline searched CASE conditions — Unwrap only the synthetic Case__Variable_N IN (...) of a simple CASE; searched CASE conditions are emitted as written (fixture test 70).
- Empty newline configuration hangs formatting — splitSqlLines returns the text unsplit when the newline option is empty.
…ps as a bare keyword
…enerated fixtures
… generated files - scripts/check-ci-matrix.ts fails CI when a package with tests is missing from the matrix - @pgsql/cli and pgsql-types get real tests and matrix entries (their test script used to exit 1) - pg-proto-parser: includeHeaderVersion option; in-repo generated sources no longer carry the generator version, so a release does not make them stale - regenerate pgsql-types narrowed types from the current field metadata - generated-files job now checks the whole tree after build + regeneration
Summary
Two deparser bugs found by round-tripping a real migration (parse → deparse → parse, compare ASTs).
1. Pretty mode indented the inside of multi-line string literals (value silently changed)
Root cause:
SqlFormatter.indentsplit on every newline. It now uses a newsplitSqlLines, which only breaks at newlines the formatter emitted — never inside'...',E'...',$tag$...$tag$, quoted identifiers or comments. One fix for every statement type. The old per-call-sitecontainsMultilineStringLiteralregex guard is removed: it missed most paths and also misfired on any two single-line literals separated by a newline, which skipped indentation entirely — hence the re-indentedCASE/ CTE /JOIN ... ONblocks in the pretty snapshots (now consistently indented).plpgsql-deparserhad the same defect in its ownindent(function bodies); it now reusessplitSqlLines, exported frompgsql-deparser.2. Parentheses around an indirection target were dropped (output did not re-parse)
A_Indirectionparenthesized only a fixed list of node types. Per the grammar, only aParamReftakes indirection bare, so every otherargis now parenthesized (ARRAY, ROW, CASE, COALESCE, boolean/null tests, JSON/XML constructors, …).Also fixed (found while broadening coverage)
ON CONFLICT ... DO UPDATE SETno longer has a trailing space.CREATE TRIGGERemitsEXECUTE FUNCTIONlike non-pretty, and no longer dropsFROM <rel>on constraint triggers.CREATE EXTENSION ... VERSIONwas emitted asNEW_VERSION ..., andALTER EXTENSION ... UPDATElostUPDATE; neither re-parsed.EXTRACT('Year' FROM d)/EXTRACT('time zone' FROM d)were emitted as a bare upper-cased word (AST change or syntax error); a quoted field now stays quoted unless it round-trips bare.CASE: aWHENvalue containing a newline leaked the internal"__Case__Variable_N__" IN (...), and anELSEwith an empty body (ELSE NULL;) was dropped, changing behaviour toCASE_NOT_FOUND.CI
pgsql-parse,plpgsql-parse,@pgsql/utils,@pgsql/cli,pgsql-types. All are in the matrix now. The last two had atestscript but no test files (jestexited 1, so rootpnpm testfailed); they get real tests — a CLI parse → deparse → parse round trip, andfield-metadata.jsonchecked against the runtime schema.scripts/check-ci-matrix.tsfails CI when a package with tests is missing from the matrix (or the matrix names a package without tests), so this can't recur.generated-filesjob runspnpm buildplus every generator and fails if the tree is dirty, so a fixture.sqlcan't be added without its tests and generated sources can't drift.pg-proto-parsergets anincludeHeaderVersionoption (defaulttrue, published behaviour unchanged). The in-repo generators turn it off, so committed generated files no longer saypg-proto-parser@1.32.5and go stale on every release.pgsql-types/src/types.tswas stale against its metadata (npm run generateis not part ofbuild); regenerated, and now covered by the check.Docs:
DEPARSER_USAGE.mdclaimed multi-line literals are preserved; the claim is now true and spells out exactly what is protected, with an example.Fixtures (SQL →
npm run kitchen-sink; both new files fail onmain's deparser)misc/multiline-string-literals.sql— 130 statements: defaults, column/table/domain CHECKs, policies, triggerWHEN, rules, INSERT/ON CONFLICT/UPDATE/DELETE/MERGE, CASE, CTEs, views, window/aggregate args, array/row literals,COMMENT ON, function bodies,E'...'and dollar quoting.misc/indirection-parens.sql— 95 statements: every target kind ×[i],[a:b],.field,.*, nested, across SELECT/DML/DDL.misc/extract.sql— quotedEXTRACTfields.plpgsql_deparser_fixes.sqltests 68–70 for literals andCASEinside nested PL/pgSQL blocks.Beyond the fixtures, a throwaway hunt (not committed) appended a multi-line suffix to every string constant, and wrapped every select-list expression in
[1],[1:2],.f1and.*, across all ~24k existing fixture statements: no pretty-only failures and no indirection failures remain.Snapshot/golden updates elsewhere (
parse,plpgsql-parse,transform) are onlyEXECUTE PROCEDURE→EXECUTE FUNCTIONand oneSET a = x,b = y→SET a = x, b = y;pgsql-types/field-metadata.jsonis regenerated from the new fixtures.Link to Devin session: https://app.devin.ai/sessions/48cf9b2b452f426abcdcd3cb3d591773
Open in Devin Desktop: https://app.devin.ai/desktop/session/48cf9b2b452f426abcdcd3cb3d591773?variant=devin
Requested by: @pyramation
🟢 Devin Review found nothing to fix · View findings · Follow Devin's work